package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.mapper.StoreMapper;
import com.itheima.restkeeper.pojo.Store;
import com.itheima.restkeeper.req.StoreVo;
import com.itheima.restkeeper.service.IStoreService;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description：门店信息账号 服务实现类
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Override
    public Page<Store> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) {
        //分页条件构建
        Page<Store> page = new Page<>();
        //按门店名称查询
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        if (!EmptyUtil.isNullOrEmpty(storeVo.getStoreName())||
            !EmptyUtil.isNullOrEmpty(storeVo.getEnableFlag())
        ) {
            queryWrapper.like(Store::getStoreName,storeVo.getStoreName())
                    .eq(Store::getEnableFlag,storeVo.getEnableFlag());
        }
        queryWrapper.orderByDesc(Store::getCreatedTime);
        //按门店状态查询
        Page<Store> storePage = page(page, queryWrapper);
        return storePage;
    }

    @Override
    public Store createStore(StoreVo storeVo) {
        Store store = BeanConv.toBean(storeVo, Store.class);
        boolean save = save(store);
        if (save) {
            return store;
        }
        //转换storeVo为Store
        return null;
    }

    @Override
    public Boolean updateStore(StoreVo storeVo) {
        Store store = BeanConv.toBean(storeVo, Store.class);

        //转换storeVo为Store
        return updateById(store);
    }

    @Override
    public Boolean deleteStore(String[] checkedIds) {
        //转换数组为集合
        List<String> list = Arrays.asList(checkedIds);
        ArrayList<Long> arrayList = new ArrayList<>();
        list.forEach(n->{
            arrayList.add(Long.valueOf(n));
        });
        //批量删除
        return removeByIds(arrayList);
    }

    @Override
    public List<Store> findStoreVoList() {
        //构建查询条件
        LambdaQueryWrapper<Store> wrapper = new LambdaQueryWrapper<>();
        //查询有效状态
        wrapper.eq(Store::getEnableFlag, SuperConstant.YES);
        return list(wrapper);
    }

}
